#include "Game.h"
#include "DXUtil.h"
#include <d3dx9.h>
#include <d3d9.h>
#include <iostream>
#include "ModelPrimitive.h"
#include "AnimChangeOverTime.h"
#include "Sphere.h"
#include "PhysicsPlane.h"


void Game::Initialize(LPDIRECT3DDEVICE9 d3ddev, LPDIRECT3D9 myDirect3D)
{
	mPhysWorld = new PhysicsWorld();
	mTotalTime = 0.0f;
	D3DXMatrixIdentity(&mIdentity);

			// Create the entity
	//m_entity=new CXFileEntity(d3ddev);
	//if (!m_entity->Load("C:\\bones.x"))
	//{
	//	delete m_entity;
	//	m_entity=0;
	//}

	IniScene(d3ddev);
	LoadContent(d3ddev);



//	m_entity->SetAnimationSet(1);
	lastTime = timeGetTime();
}

void Game::Update(LPDIRECT3DDEVICE9 d3ddev, double pElapsedTime) 
{	
	float timeElapsed= 0.037; //0.001f*(timeGetTime()-lastTime);
	//lastTime=timeGetTime();

	mTotalTime += timeElapsed;	

	D3DXMATRIX iden;
	
	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->Update(d3ddev,timeElapsed,mTotalTime);
	}

	//m_entity->FrameMove(timeElapsed,::D3DXMatrixIdentity(&iden));
	
	//bvh->Update(pElapsedTime);

	//mCamera->GetLookAt()->x = bvh->GetRoot()->GetFrames()->at(Node::mFrame)->at(0);
	//mCamera->GetLookAt()->y = bvh->GetRoot()->GetFrames()->at(Node::mFrame)->at(1);
	//mCamera->GetLookAt()->z = bvh->GetRoot()->GetFrames()->at(Node::mFrame)->at(2);

	for(int i = 0; i < mLights.size(); i++ )
	{
		if(mLights[i]->GetOn()){	
		    d3ddev->LightEnable(mLights[i]->GetLightNumber(), true);    // turn on light #0   
		}
		else if(mLights[i]->GetLightNumber() < 0 ){
			d3ddev->LightEnable(mLights[i]->GetLightNumber(),false);
		}
	}
}

D3DVECTOR* Game::DetermineEllipseTangent(D3DVECTOR* circle,D3DVECTOR* point )
{
     // This is just an implementation of the equation in the tutorial. All we need is a vector
     // giving the direction of the tangent, hence we only pay attention to the final terms.
 
     // The variable t is simply a parameter to the equation. We are only paying attention to
     // the final terms, and since they give the slope (i.e., the direction of the tangent), we
     // let t = 1.
 
   //  D3DVECTOR tangent = new D3DVECTOR();
    // tangent.x = point->x - (Ellipse->z* point->y)/Ellipse->z;
     //tangent.y = point->y + (Ellipse->z*point->x)/Ellipse->z;
	return NULL;
   //  return &tangent;     
}

void Game::IniScene(LPDIRECT3DDEVICE9 d3ddev){

	bvh = new BVHMotion();
	bvh->LoadFromFile(input);

	ModelPrimitive* tPrim = new ModelPrimitive();
	tPrim->SetSphere(d3ddev,1.5f,10,10,NULL);
	
	for(int i =0 ; i < bvh->GetOrder()->size(); i++){
		bvh->GetOrder()->at(i)->SetModel(tPrim);
	}

	mSkel = new Model();
	mSkel->GetPos()->z = 0.0f;
	mSkel->SetDir(new D3DXVECTOR3(0,0,1));
	mSkel->GetRot()->x = 0.0f;
	mSkel->SetSpeed(1.0f);
	mSkel->mAI = new AIMonster();
	mSkel->mAI->SetModel(mSkel);

	ModelPrimitive* tGround = new ModelPrimitive();
	
	tGround->SetTeapot(d3ddev,NULL);

	tGround->mAI = NULL;
	tGround->GetPos()->x = 1.0f;
	tGround->GetPos()->y = 0.0f;
	tGround->GetPos()->z = 1.0f;

	tGround->GetScale()->x = 0.25;
	tGround->GetScale()->y = 0.25;
	tGround->GetScale()->z = 0.25;


	tGround->GetMaterial()->Ambient.g = 1;	
	tGround->GetMaterial()->Ambient.r = 0;
	tGround->GetMaterial()->Ambient.b = 0;
	tGround->GetMaterial()->Ambient.a = 255;

	tGround->GetMaterial()->Diffuse.g = 1;
	tGround->GetMaterial()->Diffuse.r = 00;
	tGround->GetMaterial()->Diffuse.b = 00;
	tGround->GetMaterial()->Diffuse.a = 255;

	mSkel->mAI->GetTargets()->push_back(tGround);
	mModels.push_back(tGround);
	
	tGround = new ModelPrimitive();
	
	tGround->SetTeapot(d3ddev,NULL);
	tGround->GetScale()->x = 0.25;
	tGround->GetScale()->y = 0.25;
	tGround->GetScale()->z = 0.25;

	tGround->GetPos()->x = -1.0f;
	tGround->GetPos()->y = 0.0f;
	tGround->GetPos()->z = 3.0f;
tGround->mAI = NULL;
	tGround->GetMaterial()->Ambient.g = 1;	
	tGround->GetMaterial()->Ambient.r = 0;
	tGround->GetMaterial()->Ambient.b = 0;
	tGround->GetMaterial()->Ambient.a = 255;

	tGround->GetMaterial()->Diffuse.g = 1;
	tGround->GetMaterial()->Diffuse.r = 00;
	tGround->GetMaterial()->Diffuse.b = 00;
	tGround->GetMaterial()->Diffuse.a = 255;

	mSkel->mAI->GetTargets()->push_back(tGround);
	mModels.push_back(tGround);


	tGround = new ModelPrimitive();
	
	tGround->SetTeapot(d3ddev,NULL);
	tGround->GetScale()->x = 0.25;
	tGround->GetScale()->y = 0.25;
	tGround->GetScale()->z = 0.25;

	tGround->GetPos()->x = -2.0f;
	tGround->GetPos()->y = 0.0f;
	tGround->GetPos()->z = 5.0f;
tGround->mAI = NULL;
	tGround->GetMaterial()->Ambient.g = 1;	
	tGround->GetMaterial()->Ambient.r = 0;
	tGround->GetMaterial()->Ambient.b = 0;
	tGround->GetMaterial()->Ambient.a = 255;

	tGround->GetMaterial()->Diffuse.g = 1;
	tGround->GetMaterial()->Diffuse.r = 00;
	tGround->GetMaterial()->Diffuse.b = 00;
	tGround->GetMaterial()->Diffuse.a = 255;

	mSkel->mAI->GetTargets()->push_back(tGround);
	mModels.push_back(tGround);
	

	tGround = new ModelPrimitive();
	
	tGround->SetTeapot(d3ddev,NULL);
	tGround->GetScale()->x = 0.25;
	tGround->GetScale()->y = 0.25;
	tGround->GetScale()->z = 0.25;

	tGround->GetPos()->x = -2.0f;
	tGround->GetPos()->y = 0.0f;
	tGround->GetPos()->z = -2.0f;
tGround->mAI = NULL;
	tGround->GetMaterial()->Ambient.g = 1;	
	tGround->GetMaterial()->Ambient.r = 0;
	tGround->GetMaterial()->Ambient.b = 0;
	tGround->GetMaterial()->Ambient.a = 255;

	tGround->GetMaterial()->Diffuse.g = 1;
	tGround->GetMaterial()->Diffuse.r = 00;
	tGround->GetMaterial()->Diffuse.b = 00;
	tGround->GetMaterial()->Diffuse.a = 255;

	mSkel->mAI->GetTargets()->push_back(tGround);
	mModels.push_back(tGround);

	mModels.push_back(mSkel);
	

	mCamera = new Camera();

	D3DVIEWPORT9 view;
	d3ddev->GetViewport( &view);
	mCamera->SetAspectRatio(((float)(view.Width))/view.Height);
	
	mCamera->GetEyePos()->x = 2.0f;
	mCamera->GetEyePos()->y = 3.0f;
	mCamera->GetEyePos()->z = -3.5f;

	mCamera->SetLookAt(D3DXVECTOR3(0,0,0));

	mCamera->SetNearPlane(.0001f);

	mCamera->SetFarPlane(100000.0f);
	

	Light* mLight= new Light();

	/*mLight->GetDirection()->x = 0.0f;
	mLight->GetDirection()->y = 1.0f;
	mLight->GetDirection()->z = 0.0f;*/

	mLight->SetDirection(&D3DXVECTOR3(0,0,1));
	

	mLight->SetOn(true);
	mLights.push_back(mLight);

	for(int i = 0; i < mLights.size(); i++ )
	{
		mLights[i]->SetLightNumber(i);
		d3ddev->SetLight(mLights[i]->GetLightNumber()
						, mLight->GetD3DLight()); 
	}		
}

void Game::LoadContent(LPDIRECT3DDEVICE9 d3ddev){
	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->LoadContent(d3ddev);
	}
}

//Draws the list of models
void Game::Draw(LPDIRECT3DDEVICE9 d3ddev){
	
	d3ddev->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0,200), 1.0f, 0); // clear frame
	d3ddev->BeginScene();

	//Set Camera
	d3ddev->SetTransform(D3DTS_VIEW, mCamera->GetViewMatrix());    // set the view transform to matView

    // set the projection transform
    d3ddev->SetTransform(D3DTS_PROJECTION, mCamera->GetProjectionMatrix());    // set the projection
	
	//mMesh->Draw(d3ddev);
	//Draw Models
	for(int i = 0; i < mModels.size(); i++ )
	{
		d3ddev->SetTransform(D3DTS_WORLD, &mIdentity);
		mModels[i]->Draw(d3ddev);
	}

//	for(int i = 0; i < bvh->GetOrder()->size(); i++ )
//	{
//		d3ddev->SetTransform(D3DTS_WORLD, &mIdentity);
//		bvh->GetOrder()->at(i)->Draw(d3ddev,NULL);
//	}
	
	//bvh->Draw(d3ddev,NULL);
//m_entity->Render();
	
	d3ddev->EndScene();
}

void Game::Release(){

	for(int i = 0; i < mModels.size(); i++ )
	{
		mModels[i]->Release();
	}

}
